import { lazy } from "react";
import {
  Navigate,
  createBrowserRouter,
  RouterProvider,
  type RouteObject,
} from "react-router-dom";
import Login from "@/pages/login";
import type { AppRouteObject } from "types/router";
import { ErrorBoundary } from "react-error-boundary";

const PageError = lazy(() => import("@/pages/404/NotFound")); // 404页面

// 定义公共路由
const PUBLIC_ROUTES: AppRouteObject = {
  path: "/login",
  element: (
    <ErrorBoundary FallbackComponent={PageError}>
      <Login />
    </ErrorBoundary>
  ),
  meta: {
    key: "login",
    label: "登录",
    hideMenu: true,
  },
};
// 定义未匹配路由（404页面）
const NO_MATCHED_ROUTE: AppRouteObject = {
  path: "*",
  element: <Navigate to="/404" replace />,
};

export default function Router() {
  // 定义所有路由
  const routes = [PUBLIC_ROUTES, NO_MATCHED_ROUTE] as RouteObject[]; // 这里需要将类型断言为RouteObject[]，否则会报错
  const router = createBrowserRouter(routes);

  return <RouterProvider router={router}></RouterProvider>;
}
